package controller;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import model.DbConnection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import model.Users;
import model.ModelAkun;

/**
 *
 * @author AgusNH
 */
public class LogicAkun {

    HttpServletRequest request;

    public LogicAkun(HttpServletRequest request) {
        this.request = request;
    }

    public String register() throws Exception {
        Users user = new Users();
        String nama = request.getParameter("nama");
        String alamat = request.getParameter("alamat");
        String telepon = request.getParameter("telepon");
        String email = request.getParameter("email");
        String username = request.getParameter("username");
        String password = user.MD5(request.getParameter("password"));
        String namat = request.getParameter("namat");
        String alamatt = request.getParameter("alamatt");
        String dest = request.getParameter("dest");

        DbConnection dbConn = new DbConnection();
        ModelAkun mdReg = new ModelAkun(dbConn.getConnection());

        try {
            mdReg.insertUser(nama, alamat, telepon, email, username, password, namat, alamatt, dest);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "index.jsp";
        //String URL = "";
        //Response
    }

    public String goProfil() throws Exception {
        HttpSession session = request.getSession();
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdLogin = new ModelAkun(dbConn.getConnection());
        if (session.getAttribute("uname") != null) {
            String username = (String) session.getAttribute("uname");
            user = mdLogin.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "toko_detail.jsp";
            }
        } else {
            return "login.jsp";
        }
    }

    public String login() throws Exception {
        HttpSession session = request.getSession();
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdLogin = new ModelAkun(dbConn.getConnection());
        String username = request.getParameter("username");
        String password = user.MD5(request.getParameter("password"));
        if (username.equals("") || password.equals("")) {
            return "login.jsp";
        } else {
            user = mdLogin.getUser(username);
            if (username.equals(user.getUsername()) && password.equals(user.getPassword())) {
                session.setAttribute("uname", username);
                if (user.getAdminStat() == 1) {
                    request.setAttribute("user", user);
                    dbConn.closeConnection();
                    return "profilAdmin.jsp";
                } else {
                    request.setAttribute("user", user);
                    dbConn.closeConnection();
                    //return "barang.jsp";
                    LogicBarang go = new LogicBarang(request);
                    return (go.listBarang());
                }
            } else {
                return "index.jsp";
            }
        }

    }

    public String logout() throws Exception {
        HttpSession session = request.getSession();
        session.removeAttribute("uname");
        session.invalidate();
        return "index.jsp";
    }

    public String goEditProfil() {
        String username = request.getParameter("username");
        Users user = new Users();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());
        try {
            user = mdEdit.getUser(username);
            request.setAttribute("user", user);
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "editProfil.jsp";
    }

    public String updateProfil() throws Exception {
        HttpSession session = request.getSession();
        Users user = new Users();
        String nama = request.getParameter("nama");
        String alamat = request.getParameter("alamat");
        String telepon = request.getParameter("telepon");
        String email = request.getParameter("email");
        String username = request.getParameter("username");
        String password = user.MD5(request.getParameter("password"));
        String namat = request.getParameter("namat");
        String alamatt = request.getParameter("alamatt");
        String dest = request.getParameter("dest");
        DbConnection dbConn = new DbConnection();
        ModelAkun mdEdit = new ModelAkun(dbConn.getConnection());

        if (session.getAttribute("uname") != null) {
            mdEdit.updateUser(nama, alamat, telepon, email, password, username);
            user = mdEdit.getUser(username);
            if (user.getAdminStat() == 1) {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profilAdmin.jsp";
            } else {
                request.setAttribute("user", user);
                dbConn.closeConnection();
                return "profil.jsp";
            }
        } else {
            return "login.jsp";
        }

    }
    
    //list+delete
    public String deleteUser() {
        String idUser = request.getParameter("idUser");
        List<Users> user = new ArrayList<Users>();
        DbConnection dbConn = new DbConnection();
        ModelAkun mdHap = new ModelAkun(dbConn.getConnection());
        try {
            if (idUser == null) {
                user = mdHap.getAllUsers();
                request.setAttribute("users", user);
            } else {
                int id = Integer.parseInt(idUser);
                mdHap.deleteUser(id);
                user = mdHap.getAllUsers();
                request.setAttribute("users", user);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            dbConn.closeConnection();
        }
        return "listUsers.jsp";
    }
}
